from manim import *
class CustomCurve(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [-7, 0, 0],
            [-15,-5,0],
            [-7,-10,0],
        ]
        self.set_points_smoothly(points)
class CustomCurve1(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [28, -5, 0],
            [29,-16,0],
            [40,-17,0]
        ]
        self.set_points_smoothly(points)

class CustomCurve2(VMobject):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # 定义自定义曲线的控制点
        points = [
            [26, -5, 0],
            [27, -17, 0],
            [29,-20,0],
            [35,-15,0],
            [40,-13,0],
            
        ]
        self.set_points_smoothly(points)

class math1(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #镜头跟进效果 
        self.camera.frame.save_state()
         #题目出现
        text = Tex(r"\text{(张1000题6)已知$\int _{0}^{2}\dfrac{1}{\left| \ln x\right| ^{a}}dx$\text{收敛,a为常数,则：}}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,20,0])
        text1 = Tex(
                   r"\text{(A)}$1 <a\leq 2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,15,0])
        text2 = Tex(
                    r"\text{(B)}$a <1$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,15,0])
        text3 = Tex(
                   r"\text{(C)}$1\leq a <2$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-22,8,0])
        text4 = Tex(
                    r"\text{(D)}$a >2 $"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([22,8,0])

        self.play(Write(text),Write(text1),Write(text2),Write(text3),Write(text4))
        self.wait(0.1)
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        #self.play(text.animate().move_to([0,20,0]),text1.animate().move_to([-22,15,0]) ,text2.animate().move_to([22,15,0]),text3.animate().move_to([-22,8,0]),text4.animate().move_to([22,8,0]) )

        text1 = Tex(
                    r"\text{瑕点为}$0$\text{和}$1$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-36,0,0])
        self.play(Write(text1))

        text2 = Tex(
                    r"\text{当}$x\rightarrow 0 $",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-5,0])
        self.play(Write(text2))
        text3 = Tex(
                    r"$\dfrac{1}{\left| \ln x\right| ^{a}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25,-5,0])
        self.play(Write(text3))
        text4 = Tex(
                    r"\text{极限等价？}",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-5,0])
        self.play(Write(text4))
        self.play(FadeOut(text4))
        #画曲线
        curve = CustomCurve(color=RED, stroke_width=20)
        
        # 添加到场景并播放动画
        self.play(Create(curve))
        self.wait(0.5)
        text5 = Tex(
                    r"$a<0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,0,0])
        text6 = Tex(
                    r"$a=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-5,0])
        text7 = Tex(
                    r"$a>0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-2,-10,0])
        self.play(Write(text5),Write(text6),Write(text7))

        text8 = Tex(
                    r"\text{与}$\left| \ln x\right| ^{-\alpha }$\text{同趋势}",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([15,0,0])
        self.play(Write(text8))
          # 创建坐标系
        axes = Axes(
            x_range=[-3, 10, 3],  # 设置 x 轴范围，从 -π 到 π，步长为 π
            y_range=[-3, 10, 3],  # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE, "stroke_width": 30},  # 设置坐标轴的颜色和线宽
            x_length=20,  # 控制 x 轴长度
            y_length=20,  # 控制 y 轴长度
        )
        axes.move_to([30, -15, 0])  # 移动坐标系的位置

        # 绘制 1/x 函数图像
        # 分为两部分以避开 x=0
        #graph_left = axes.plot(lambda x: 5/x, color=BLUE, stroke_width=30, x_range=[-5, -0.1])
        #graph_right = axes.plot(lambda x: 5/x, color=PURE_RED, stroke_width=30, x_range=[0.1, 10])
        
       

        title = Tex(
                    r"$\dfrac{1}{x}$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,0,0])

       


        #self.play(Create(arrow10),Create(text15))
        # 将所有元素添加到场景中
        #self.play(Create(axes), Create(labels), Create(graph2))
         # 显示图像
        curve1 = CustomCurve1(color=PURE_RED, stroke_width=20)
        
        self.play(Create(axes))
        self.play( Write(title), Create(curve1))

        title1 = Tex(
                    r"$\left| \ln x\right| ^{-\alpha }$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([35,-7,0])

        curve2 = CustomCurve2(color=BLUE, stroke_width=20)
        self.play( Write(title1), Create(curve2))

        text9 = Tex(
                    r"\text{(收敛)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,0,0])
        self.play(Write(text9))

        text10 = Tex(
                    r"$\int _{0}^{1}1dx$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-15,0])
        self.play(Write(text10))
        text11 = Tex(
                    r"\text{(收敛)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-5,0])
        self.play(Write(text11))
        text12 = Tex(
                    r"$\int ^{1}_{0}\dfrac{1}{\left| \ln x\right| ^{a}}dx$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-15,-15,0])
        self.play(ReplacementTransform(text10,text12))


        rectangle=Rectangle(color=BLUE,fill_opacity=0.0,width=2.4,height=8
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([-20.2, -15, 0])
        self.play(Create(rectangle))
        text13 = Tex(
                    r"$\lim\limits _{x\rightarrow 0}\dfrac{1}{\left| \ln x\right| ^{a}}=0$",color=YELLOW
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,-15,0])
        self.play(Write(text13))
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.0,width=8,height=8
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle1.move_to([-15, -15, 0])
        self.play(Create(rectangle1))


        text14 = Tex(
                    r"\text{(收敛)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,-10,0])
        self.play(Write(text14))
        text15 = Tex(
                    r"$a\in R$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,12,0])
        self.play(Write(text15))
        self.wait(0.5)
        self.play(Uncreate(text14),Uncreate(text13),Uncreate(text12),Uncreate(text11)
                  ,Uncreate(text9),Uncreate(text8),Uncreate(text7)
                  ,Uncreate(text6)
                  ,Uncreate(text5),Uncreate(text3)
                  ,Uncreate(text2),Uncreate(curve),Uncreate(curve1),Uncreate(curve2)
                  ,Uncreate(title),Uncreate(title1),FadeOut(rectangle),FadeOut(rectangle1)
                  ,Uncreate(axes))
        text2 = Tex(
                    r"\text{当}$x\rightarrow 1$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-5,0])
        self.play(Write(text2))
        text3 = Tex(
                    r"$\dfrac{1}{\left| \ln x\right| ^{a}}$",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-28,-5,0])
        self.play(Write(text3))
        text4 = Tex(
                    r"\text{等价于}$\dfrac{1}{\left( x-1\right) ^{\alpha }}$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,-5,0])
        self.play(Write(text4))
        
        
        text9 = Tex(
                    r"\text{令}$x-1=t$",color=BLUE
                   ,tex_template=TexTemplateLibrary.ctex).scale(8.0).move_to([17, -5, 0])
        self.play(Write(text9))

        text2 = Tex(
                    r"\text{当}$t\rightarrow 0$",color=RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-40,-15,0])
        self.play(Write(text2))
        text3 = Tex(
                    r"$\dfrac{1}{t^{a}}$",color=PURPLE
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-28,-15,0])
        self.play(Write(text3))
        text3 = Tex(
                    r"\text{a<1时(收敛)}",color=GREEN
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(Write(text3))
        
        text19 = Tex(
                    r"$a<1$",color=PURE_RED
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,8,0])
        self.play(Write(text19))
        self.wait(0.5)
        #半透明矩阵扩展
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=YELLOW,stroke_width=0).move_to([22,15,0])
        self.play(
            rectangle4.animate.stretch(16.0,dim=0)
        )
        
        self.wait()